//
//  JGDealsViewController.m
//  JGMeiTuanHD
//
//  Created by JJetGu on 15/9/19.
//  Copyright (c) 2015年 Free. All rights reserved.
//

#import "JGDealsViewController.h"
#import "MJExtension.h"
#import "MJRefresh.h"
#import "JGConst.h"
#import "UIView+AutoLayout.h"
#import "JGDeal.h" 
#import "JGDealCell.h"
#import "UIView+Extension.h"
#import "MBProgressHUD+MJ.h"
#import "JGDetailViewController.h"

@interface JGDealsViewController () <JGDealCellDelegate>

/** 没有团购数据时的提示背景图 */
@property (nonatomic, weak) UIImageView *noDataView;

@end

@implementation JGDealsViewController

static NSString * const reuseIdentifier = @"deal";

-(NSMutableArray *)deals
{
    if (!_deals) {
        self.deals = [[NSMutableArray alloc] init];
    }
    
    return _deals;
}

-(UIImageView *)noDataView
{
    if (!_noDataView) {
        // 添加一个"没有数据"的提醒
        UIImageView *noDataView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.emptyIcon]];
        [self.view addSubview:noDataView];
        [noDataView autoCenterInSuperview];
        self.noDataView = noDataView;
    }
    
    return _noDataView;
}

-(instancetype)init
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(305, 305);
    return [self initWithCollectionViewLayout:layout];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置背景色
    self.collectionView.backgroundColor = JGGlobalBg;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"JGDealCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    
     self.collectionView.alwaysBounceVertical = YES;

}

/**
 当屏幕旋转,控制器view的尺寸发生改变调用
 */
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    int cols = (size.width == 1024)? 3 : 2;
    
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
    CGFloat inset = (size.width - cols * layout.itemSize.width) / (cols + 1);
    layout.sectionInset = UIEdgeInsetsMake(inset, inset, inset, inset);
    
    // 设置每一行之间的间距
    layout.minimumLineSpacing = inset;
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    // 计算一遍内边距
    [self viewWillTransitionToSize:CGSizeMake(collectionView.width, 0)  withTransitionCoordinator:nil];
    
    // 控制"没有数据"的提醒
    self.noDataView.hidden = (self.deals.count != 0);
    
    return self.deals.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    JGDealCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.delegate = self;
    cell.deal = self.deals[indexPath.item];
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    JGDetailViewController *detailVc = [[JGDetailViewController alloc] init];
    detailVc.deal = self.deals[indexPath.item];
    [self presentViewController:detailVc animated:YES completion:nil];
}

@end



